/**************************************************************************
 * @name: captcha2bmp.c
 * @brief: 随机4位验证码保存到bmp图片上
 * @date: 2025/02/11
 * @author: boluzs@163.com
 * @note: None
 * @version: 1.0.0
 * @Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include "font.h"

#define BMP_PATH "./empty.bmp"
#define BMP_SIZE 60 * 40 * 4
char buf[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char captcha[5] = {0};
font *pft;
unsigned int *bmp_buf;

int Show_Font(unsigned int fontSize, u32 width, u32 height,
              u32 byteperpixel, color buf_c, s32 x, s32 y,
              char *text, color font_c, s32 maxWidth, int px, int py)
{
    fontSetSize(pft, fontSize);                                            // 字体大小
    bitmap *bm = createBitmapWithInit(width, height, byteperpixel, buf_c); // 申请缓存
    fontPrint(pft, bm, x, y, text, font_c, maxWidth);                      // 加载字体
    show_font_to_lcd(bmp_buf, px, py, bm);                                 // 刷新缓存
    destroyBitmap(bm);                                                     // 释放输出框内存
    return 0;
}

/**************************************************************************
 * @name: rand_captcha
 * @brief: 生成随机4位验证码
 * @note: None
 * @retval: None
 **************************************************************************/
void rand_captcha(void)
{
    // 1.生成随机种子
    srand(time(NULL));
    for (size_t i = 0; i < 4; i++)
    {
        captcha[i] = buf[rand() % strlen(buf)];
    }
    printf("生成出来的验证码是：%s\n", captcha);
}

/**************************************************************************
 * @name: captcha2bmp
 * @brief: 随机4位验证码保存到bmp图片上
 * @note: None
 * @retval: None
 **************************************************************************/
void captcha2bmp(void)
{
    // 1.打开bmp图片
    FILE *fp = fopen(BMP_PATH, "r+");
    if (NULL == fp)
    {
        fprintf(stderr, "open error, errno= %d,%s! \n", errno, strerror(errno));
    }
    // 2.跳过前面54个字节
    fseek(fp, 54, SEEK_SET);
    // 3. 将生成的验证码存到bmp_buf中
    Show_Font(22, 60, 40, 4, getColor(0, 255, 255, 255), 0, 0, captcha, getColor(0, 255, 255, 255), 60, 20, 20);
    fwrite(bmp_buf, 1, BMP_SIZE, fp);
    // fontSetSize(pft, 22);                                                     // 字体大小
    // bitmap *bm = createBitmapWithInit(60, 40, 4, getColor(0, 255, 255, 255)); // 申请缓存
    // for (size_t i = 0; i < 4; i++)
    // {
    //     fontPrint(pft, bm, 0, 0, "金额", getColor(0, 255, 255, 255), 60); // 加载字体
    //     show_font_to_lcd(buf, 20, 20, bm);                                // 刷新缓存
    // }
    // destroyBitmap(bm);
    fclose(fp);
}

int main(int argc, char const *argv[])
{
    // 打开字库
    pft = fontLoad("/usr/share/fonts/truetype/ubuntu/Ubuntu-BI.ttf");
    bmp_buf = (unsigned int *)calloc(1, BMP_SIZE);
    rand_captcha();
    captcha2bmp();
    free(bmp_buf);
    return 0;
}
